//
//  Coding_NetAPIManager.m
//  百车易车险
//
//  Created bychenxu on 14-7-30.
//  Copyright (c) 2014年 chenxu. All rights reserved.
//

#import "NetAPIManager.h"
#import "Insurance.h"
#import "JSONKit.h"
#import "BLogin.h"
@implementation NetAPIManager
+ (instancetype)sharedManager {
    static NetAPIManager *shared_manager = nil;
    static dispatch_once_t pred;
	dispatch_once(&pred, ^{
        shared_manager = [[self alloc] init];
    });
	return shared_manager;
}

#pragma mark Login
- (void)request_Login_WithParams:(id)params andBlock:(void (^)(id data, NSError *error))block{
    
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"login" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        //Data键的内容是个数组
        NSArray *resultData = [data valueForKeyPath:@"Data"];
        NSString *success = [[data valueForKey:@"Success"] stringValue];
        
        //登陆成功
        if ([success  isEqual: @"1"]) {
            NSDictionary *detaildata = [resultData objectAtIndex:0];
            [Login doLogin:detaildata];
            //json 转化成user的model
           // User *curLoginUser = [NSObject objectOfClass:@"User" fromJSON:detaildata];
            block(success, nil);
        }
        //用户密码错&已经登陆
        else{
            block(success, error);
        }
    }];
}

- (void)request_Register_WithParams:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"adduser" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
    
        //处理错误情况
        BOOL resultData = [[data valueForKeyPath:@"Success"]boolValue];
        if (resultData == true ) {
            block(@"success", nil);
        }else{
            block(@"fail", nil);
        }
    }];
}

- (void)request_Logout_WithParams:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"logout" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        //处理错误情况
        BOOL resultData = [[data valueForKeyPath:@"Success"]boolValue];
        if (resultData == 1) {
            [Login doLogout];
            block(@"success", nil);
        }else{
            block(@"fail", nil);
        }
    }];
}



- (void)request_limitNumber:(id)params andBlock:(void (^)(id data, NSError *error))block;{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"limitnumber" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        if (data) {
            block(data, nil);
        }else{
            block(nil, error);
        }
    }];
}

#pragma mark 车辆列表
- (void)request_cars:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"returncar" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        if (data) {
            block(data, nil);
        }else{
            block(nil, error);
        }
    }];
}

- (void)add_car:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"addcar" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        block(data,error);
    }];
}

- (void)modify_car:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"modifycar" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        bool success = [data valueForKeyPath:@"Success"];
        if (success) {
            block(@"success", nil);
        }else{
            block(nil, error);
        }
    }];
}

- (void)delete_car:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"deletecar" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        bool success = [data valueForKeyPath:@"Success"];
        if (success) {
            block(@"success", nil);
        }else{
            block(nil, error);
        }
    }];
}





- (void)request_company:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"getcompanyname" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        id companies = [data valueForKeyPath:@"Data"];
        if (companies) {
            block(companies, nil);
        }else{
            block(@"fail", error);
        }
    }];
}

#pragma mark 个人信息

- (void)edit_UserInfo:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"editpeople" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        if (success) {
            block(@"success", nil);
        }else{
            block(@"fail", error);
        }
    }];
}

- (void)update_password:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"repassword" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        NSString * msg = [data valueForKey:@"Msg"];

        if (success) {
            block(msg, nil);
        }else{
            block(msg, error);
        }
    }];
}

- (void)update_id:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"updateid" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        id datas = [data valueForKey:@"Data"];
        
        if (success) {
            block(datas, nil);
        }else{
            block(nil, error);
        }
    }];
}
#pragma mark Order
/*!
 *  @brief  未完成订单
 *  @param params Username
 *  @param block  回调
 */
- (void)request_unfinishedorder:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"unfinisheduserorder" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        id datas = [data valueForKey:@"Data"];
        
        if (success) {
            block(datas, nil);
        }else{
            block(nil, error);
        }
    }];
}

/*!
 *  @brief  完成的订单
 *  @param params Username
 *  @param block  回调
 */
- (void)request_finishedorder:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"finisheduserorder" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        id datas = [data valueForKey:@"Data"];
        
        if (success) {
            block(datas, nil);
        }else{
            block(nil, error);
        }
    }];
}

/*!
 *  @brief  取消订单
 *  @param params Orderid
 *  @param block  回调
 */

- (void)delete_order:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"deleteuserorder" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        if (success) {
            block(data, nil);
        }else{
            block(nil, error);
        }
    }];
}

/*!
 *  @brief  提交保险订单--->转化成已完成订单
 *  @param params <#params description#>
 *  @param block  <#block description#>
 */
- (void)submit_order:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"getpersonalinfo" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        block(data,nil);
    }];
}


/*!
 *  @brief  完成订单
 *  @param params <#params description#>
 *  @param block  <#block description#>
 */
- (void)finish_order:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"payfinished" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        block(data,nil);

    }];
}


/*!
 *  @brief  用户反馈
 *  @param params <#params description#>
 *  @param block  <#block description#>
 */
- (void)send_feedback:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"] boolValue];
        id datas = [data valueForKey:@"Data"];
        
        if (success) {
            block(datas, nil);
        }else{
            block(nil, error);
        }
    }];
}

/*!
 *  @brief  获取报价
 *  @param params <#params description#>
 *  @param block  <#block description#>
 */
- (void)request_price:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"userorder" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"] boolValue];
        
        if (success) {
            block(data, nil);
        }else{
            block(nil, error);
        }
    }];
}

/*!
 *  @brief  刷新报价
 *  @param params <#params description#>
 *  @param block  成功后返回一个Insurance数组
 */
- (void)refresh_price:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"userrefresh" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        
        id Data = [data valueForKeyPath:@"Data"];
       
        if (Data) {
            
            NSString* xianzhong = [Data valueForKey:@"Xianzhong"];
            xianzhong = [xianzhong stringByReplacingOccurrencesOfString:@"[" withString:@""];
            xianzhong = [xianzhong stringByReplacingOccurrencesOfString:@"]" withString:@""];
            
 
            NSArray* array = [xianzhong componentsSeparatedByString:@"},"];
            //处理成字典的array
            NSMutableArray* result = [[NSMutableArray alloc]init];
            for (int i = 0; i<[array count]-1; i++) {
                NSString* temp = [[array objectAtIndex:i]stringByAppendingString:@"}"];
                Insurance* ins = [NSObject objectOfClass:@"Insurance" fromJSON: [temp objectFromJSONString]];
                [result addObject:ins];
            }
            
            block(result, nil);
        }else{
            block(nil, error);
        }
    }];
}






/*!
 *  @brief  用户端   获取订单对应的商家的百度id
 *  @param params getuserid
 *  @param block  <#block description#>
 */
- (void)user_request_baiduid:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"getbusinessid" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        id Data = [data valueForKeyPath:@"Data"];
        
        if (Data) {
            block(Data, nil);
        }else{
            block(nil, error);
        }
    }];
}

/*!
 *  @brief  商家端   获取订单对应的用户的百度id
 *  @param params <#params description#>
 *  @param block  <#block description#>
 */
- (void)company_request_baiduid:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"getuserid" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        id Data = [data valueForKeyPath:@"Data"];
        
        if (Data) {
            block(Data, nil);
        }else{
            block(nil, error);
        }
    }];
}

/*!
 *  @brief  保存用户聊天信息
 *  @param params <#params description#>
 *  @param block  <#block description#>
 */
- (void)update_talk:(id)params andBlock:(void (^)(id data, NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"talk" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        id time = [data valueForKeyPath:@"Time"];
        
        if (time) {
            block(time, nil);
        }else{
            block(nil, error);
        }
    }];
}

#pragma mark 图像上传下载
/*!
 *  @brief  上传用户头像
 *  @param image    <#image description#>
 *  @param param    <#param description#>
 *  @param success  <#success description#>
 *  @param failure  <#failure description#>
 *  @param progress <#progress description#>
 */
- (void)uploadUserIconImage:(UIImage *)image
                  withParam:(NSDictionary*)param
               successBlock:(void (^)(NSString *imagePath))success
               failureBlock:(void (^)(NSError *error))failure
              progerssBlock:(void (^)(CGFloat progressValue))progress{
    [[NetAPIClient sharedJsonClient] uploadImage:image
                                            path:@"shangchuantouxiang"
                                            name:@"file"
                                       withParam:(NSDictionary*)param
                                    successBlock:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *reslutString = [responseObject objectForKey:@"data"];
        DebugLog(@"%@", reslutString);
        success(reslutString);
    } failureBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(error);
    } progerssBlock:^(CGFloat progressValue) {
        progress(progressValue);
    }];
}


/*!
 *  @brief  更换用户头像
 *  @param image    目标图像
 *  @param param    附加参数
 *  @param success  成功回调
 *  @param failure  失败回调
 *  @param progress 进度回调
 */

- (void)request_UpdateUserIconImage:(UIImage *)image
                          withParam:(NSDictionary*)param
                       successBlock:(void (^)(id responseObj))success
                       failureBlock:(void (^)(NSError *error))failure
                      progerssBlock:(void (^)(CGFloat progressValue))progress{

    [self uploadUserIconImage:image
                    withParam:(NSDictionary*)param
                 successBlock:^(NSString *imagePath) {
                     //成功后回调
                     success(image);
                     
    } failureBlock:failure progerssBlock:progress];
}

/*!
 *  @brief  读取服务器图像
 *  @param imageUrlStr 完整url地址
 *  @param block       回调
 */
- (void)loadImageWithPath:(NSString *)imageUrlStr completeBlock:(void (^)(UIImage *image, NSError *error))block{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrlStr]];
    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Response: %@", responseObject);
        block(responseObject, nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Image error: %@", error);
        block(nil, error);
    }];
    [requestOperation start];
}

#pragma mark - 商家
- (void)company_Logout_WithParams:(id)params andBlock:(void (^)(id data, NSError *error))block{
//    params = @{
//               @"Businessname" :@"rrrrrr"
//               };
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"businesslogout" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        //处理错误情况
        BOOL resultData = [[data valueForKeyPath:@"Success"]boolValue];
        if (resultData == 1) {

            block(@"success", nil);
        }else{
            block(@"fail", nil);
        }
    }];
}

- (void)company_Login_WithParams:(id)params andBlock:(void (^)(id data, NSError *error))block{
    
//    params  = @{
//                @"Businessname":@"rrrrrr",
//                @"Businesspassword":@"rrrrrr"
//                };
    
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"businesslogin" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        //处理错误情况
        int success = [[data valueForKeyPath:@"Success"]intValue];
        
        if (success == 1) {
            NSDictionary *detaildata = [data objectForKey:@"Data"];
            [BLogin doLogin:detaildata];
            block(data, nil);
        }else{
            [self showHudTipStr:[data valueForKey:@"Msg"]];
            block(data, nil);
        }
    }];
    
}


- (void)company_request_unfinishedorder:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"unfinishedbusinessorder" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        id datas = [data valueForKey:@"Data"];
        
        if (success) {
            block(datas, nil);
        }else{
            block(nil, error);
        }
    }];
}
- (void)company_request_finishedorder:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"finishedbusinessorder" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        id datas = [data valueForKey:@"Data"];
        
        if (success) {
            block(datas, nil);
        }else{
            block(nil, error);
        }
    }];
}

- (void)company_baojia:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"baojia" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        
        if (success) {
            block(data, nil);
        }else{
            block(nil, error);
        }
    }];
}

- (void)request_finishedorderinfo:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"finisheduserorder" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        
        if (success) {
            block(data, nil);
        }else{
            block(nil, error);
        }
    }];
}

- (void)company_request_finishedorderinfo:(id)params andBlock:(void (^)(id data,NSError *error))block{
    [[NetAPIClient sharedJsonClient] requestJsonDataWithPath:@"finishedbusinessorder" withParams:params withMethodType:Post andBlock:^(id data, NSError *error) {
        
        BOOL success = [[data valueForKeyPath:@"Success"]boolValue];
        
        if (success) {
            block(data, nil);
        }else{
            block(nil, error);
        }
    }];
}


- (NSString*)getAvatarUrl:(NSString*)username{
    
    return  [NSString stringWithFormat:@"%@%@.jpg",kNetPath_ImgBase,username];
}
@end